<?php

namespace app\admin\model\order;

use app\admin\model\PinkConfig;
use app\admin\model\store\GoodsSku;
use app\admin\model\user\User;
use app\common\model\TimeModel;
use think\facade\Db;

class Order extends TimeModel
{

    protected $name = "order";

    protected $autoWriteTimestamp = true;

    protected $deleteTime = "delete_time";

    //关联订单详情表数据
    public function orderGoodsInfo()
    {
        return $this->hasMany(OrderGoodsInfo::class, 'order_id', 'id');
    }
    //关联用户信息
    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id')->bind(['nickname','avatar']);
    }
    //关联商品
    public function goodsSku()
    {
        return $this->belongsTo(GoodsSku::class,'goods_sku_id','id');
    }

    //关联拼团配置表
    public function pinkConfig()
    {
        //绑定夫模型字段
        $bind = [
            'pink_name',
            'goods_img',
            'goods_name',
            'goods_sku_price',
            'goods_sku_id',
            'pink_price',
            'pink_people',
            'end_time',
            'start_time'
        ];
        return $this->belongsTo(PinkConfig::class, 'pink_id', 'id')->bind($bind);
    }

    //分页查询列表
    public static function pageOrderList($pagesize)
    {
        return self::paginate($pagesize);
    }

    // 统计订单分类数据
    public static function orderTypeCount()
    {
        $data['general'] = self::where(['pink_id' => 0, 'seckill_id' => 0, 'bargain_id' => 0, 'is_system_del' => 0])->count();
        $data['pink'] = self::where('pink_id', '>', 0)->where('is_system_del', 0)->count();
        $data['seckill'] = self::where('seckill_id', '>', 0)->where('is_system_del', 0)->count();
        $data['bargain'] = self::where('bargain_id', '>', 0)->where('is_system_del', 0)->count();
        return $data;
    }

    /**
     * Author:YangXinPeng
     * 针对订单并发处理
     * @param $goods_sku_id
     * @return mixed
     */
    public static function orderNum($goods_sku_id)
    {
        //商品skuid  校验商品数量
        $res = Db::query("select sku_stock from ea_goods_sku where id=:id for update", ['id' => $goods_sku_id]);
        return $res[0]['sku_stock'];
    }

    //查询订单状态数量
    public static function orderStatusCount()
    {
        //未支付
        $data['unpay_status'] = self::where('pay_status', 0)->where('status', 0)->where('refund_status', 0)->where('is_del', 0)->count();
        //未发货
        $data['not_shipped'] = self::where('pay_status', 1)->where('status', 0)->where('shipping_type', 1)->where('refund_status', 0)->count();
        //待收货
        $data['waits_delivery'] = self::where('pay_status', 1)->where('status', 1)->where('shipping_type', 1)->where('refund_status', 0)->where('is_del', 0)->count();
        //待评价
        $data['evaluate'] = self::where('pay_status', 1)->where('status', 2)->where('refund_status', 0)->where('is_del', 0)->count();
        //交易完成
        $data ['complete'] = self::where('pay_status', 1)->where('status', 3)->where('refund_status', 0)->where('is_del', 0)->count();
        //待核销
//        $data['waiting_for_write_off'] = self::where('pay_status', 1)->where('status', 0)->where('shipping_type', 2)->where('refund_status', 0)->where('is_del', 0)->count();
        //已核销
//        $data['written_off'] = self::where('pay_status', 1)->where('refund_status', 0)->where('is_del', 0)->count();
        //已删除
        $data ['is_del'] = self::where('is_del', 1)->count();
        return $data;
    }

    /**
     * 根据不同的订单状态获取订单信息 ，如果状态值为0 或者空 获取的是所有的订单数据
     * Author:William
     * @param null $status
     * @param null $model
     * @return Order|mixed|null
     */
    public static function statusByWhere($status, $model = null)
    {
        if ($model == null) {
            $model = new self;
        }
        if ('' === $status) {
            return $model;
        } elseif
        ($status == 8) {
            return $model;
        } elseif
        ($status == 0) {//未支付
            return $model->where('pay_status', 0)->where('status', 0)->where('refund_status', 0)->where('is_del', 0);
        } elseif
        ($status == 1) {//已支付 未发货
            return $model->where('pay_status', 1)->where('status', 0)->where('shipping_type', 1)->where('refund_status', 0)->where('is_del', 0);
        } elseif
        ($status == 2) {//已支付  待收货
            return $model->where('pay_status', 1)->where('status', 1)->where('shipping_type', 1)->where('refund_status', 0)->where('is_del', 0);
        } elseif
        ($status == 5) {//已支付  待核销
            return $model->where('pay_status', 1)->where('status', 0)->where('shipping_type', 2)->where('refund_status', 0)->where('is_del', 0);
        } elseif
        ($status == 3) {// 已支付  已收货  待评价
            return $model->where('pay_status', 1)->where('status', 2)->where('refund_status', 0)->where('is_del', 0);
        } elseif
        ($status == 4) {// 交易完成
            return $model->where('pay_status', 1)->where('status', 3)->where('refund_status', 0)->where('is_del', 0);
        } elseif
        ($status == -1) {//退款中
            return $model->where('pay_status', 1)->where('refund_status', 1)->where('is_del', 0);
        } elseif
        ($status == -2) {//已退款
            return $model->where('pay_status', 1)->where('refund_status', 2)->where('is_del', 0);
        } elseif
        ($status == -3) {//退款
            return $model->where('pay_status', 1)->where('refund_status', 'in', '1,2')->where('is_del', 0);
        } elseif
        ($status == -4) {//已删除
            return $model->where('is_del', 1);
        } elseif
        ($status == 8) {//已卖出
            return $model->where('pay_status', 1)->where('refund_status', 0)->where('is_del', 0);
        } else {
            return $model;
        }
    }


    /*用户申请退款*/
    public static function refund($refundInfo = [])
    {
        return self::where('order_no', $refundInfo['order_no'])->update($refundInfo);
    }

    //删除订单
    public static function del($id)
    {
        $res = self::where('is_del', '>', '0')->destroy($id);
        return $res;
    }


}